package hello;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

import hello.mapper.StaffMapper;
import hello.model.Staff;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

@RestController
public class StaffController {
    Logger logger = Logger.getLogger(this.getClass());

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @Autowired
    private StaffMapper staffMapper;

    @RequestMapping(value = "/staffs" ,method = RequestMethod.GET)
    public List<Staff> getStaffs() {
        List<Staff> staffs = staffMapper.getStaffs();
        return staffs;
    }

    @RequestMapping(value = "/staff/add")
    public HashMap addStaff() {
        Long randomn = new Random(20000000).nextLong();
        Staff staff = new Staff();
        staff.setStaffName("测试号"+randomn.toString().substring(0, 10));
        staff.setStaffCode("staffCode");
        staff.setCompanyId(1L);
        staff.setCreateBy(2L);
        staff.setCreateTime(new Date());
        staff.setDepartmentId(3L);
        staff.setJobState("1");
        staff.setPostId(4L);
        staff.setPositionId(5L);
        staff.setUpdateTime(new Date());
        staffMapper.addStaff(staff);
        return new HashMap() {{
            put("result", 1);
            put("id", staff.getId());
        }};
    }

    @RequestMapping(value = "/staff/update/{id}")
    public HashMap updateStaff(@PathVariable Long id, @RequestParam("dept_id") Long deptId) {
        RequestContextHolder.currentRequestAttributes().setAttribute("user_id", "233333", RequestAttributes.SCOPE_REQUEST);
        RequestContextHolder.currentRequestAttributes().setAttribute("tenant_id", "400", RequestAttributes.SCOPE_REQUEST);

        Staff staff = staffMapper.getById(id);
        staff.setDepartmentId(deptId);
        Long randomn = new Random(20000000).nextLong();
        staff.setStaffName("测试号"+randomn.toString().substring(0, 10));
        staffMapper.updateDeptId(staff);
        logger.info(staff.getStaffName());
        return new HashMap() {{
            put("result", 1);
        }};
    }


    @RequestMapping(value = "/staff/delete/{id}")
    public HashMap deleteStaff(@PathVariable Long id) {
        RequestContextHolder.currentRequestAttributes().setAttribute("user_id", "233333", RequestAttributes.SCOPE_REQUEST);
        RequestContextHolder.currentRequestAttributes().setAttribute("tenant_id", "400", RequestAttributes.SCOPE_REQUEST);
        Staff staff = staffMapper.getById(id);
        if(staff != null){
            logger.info(staff.getStaffName());
            staffMapper.deleteById(staff);
        }
        return new HashMap() {{
            put("result", 1);
        }};
    }
}
